//
//  AlbumsViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-7.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "MyPhotoViewController.h"
#import "ComposePhotoViewController.h"
#import "PhotoView.h"

@interface MyPhotoViewController()<UIActionSheetDelegate>

@property (strong, nonatomic) UIActionSheet *actionSheet;
@property (strong, nonatomic) UIImagePickerController *imagePicker;

@end

@implementation MyPhotoViewController

- (id)init
{
    self = [super initWithNibClass:[MyPhotoViewController class]];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self createNavigation:@"拍客"];
    
    _actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    
    self.allPhotoView = [self createViewWithType:0];
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)changeViewAction:(id)sender
{
    _allPhotoBtn.selected = _envPhotoBtn.selected = _dishPhotoBtn.selected = NO;
    [sender setSelected:YES];
    
    if ([sender tag] == 0) {
        if (_envPhotoView) [_envPhotoView removeFromSuperview];
        if (_dishPhotoView) [_dishPhotoView removeFromSuperview];
        
        if (!_allPhotoView) {
            self.allPhotoView = [self createViewWithType:0];
        }
        if (!_allPhotoView.superview) {
            [self.view addSubview:_allPhotoView];
        }
    }
    
    if ([sender tag] == 1) {
        if (_allPhotoView) [_allPhotoView removeFromSuperview];
        if (_dishPhotoView) [_dishPhotoView removeFromSuperview];
        
        if (!_envPhotoView) {
            self.envPhotoView = [self createViewWithType:1];
        }
        if (!_envPhotoView.superview) {
            [self.view addSubview:_envPhotoView];
        }
    }
    
    if ([sender tag] == 2) {
        if (_allPhotoView) [_allPhotoView removeFromSuperview];
        if (_envPhotoView) [_envPhotoView removeFromSuperview];
        
        if (!_dishPhotoView) {
            self.dishPhotoView = [self createViewWithType:2];
        }
        
        if (!_dishPhotoView.superview) {
            [self.view addSubview:_dishPhotoView];
        }
    }
}

- (PhotoView *)createViewWithType:(int)type
{
    float height = ScreenHeight-NavBarHeight-StatusBarHeight-_toolView.height;
    CGRect frame = CGRectMake(0, _toolView.bottom, ScreenWidth, height);
    
    PhotoView *photoView = [[PhotoView alloc] initWithFrame:frame userName:[AppSetting userName] type:type];
    photoView.isUserCenter = YES;
    photoView.tag = type;
    photoView.photoVC = self;
    
    [self.view addSubview:photoView];
    
    return photoView;
}

- (IBAction)takePhotoAction:(id)sender
{
    if (![self.actionSheet isVisible]) {
        [self.actionSheet showInView:[UIApplication sharedApplication].keyWindow];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex){
        case 0: {
            [self showImagePicker:NO];
            break;
        }
        case 1: {
            [self showImagePicker:YES];
            break;
        }
    }
}

- (void)showImagePicker:(BOOL)isCamera
{
    if (isCamera) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        self.imagePicker.allowsEditing = NO;
    }else{
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    // do nothing here
}

// 完成选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker
{
    [self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)_picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    if (_picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        _picker.title = @"拍照";
        _picker.allowsEditing = NO;
    }
	
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication  sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        
        UIImage *imageCopy = [image fixOrientation];
        [self saveImage:imageCopy WithName:TempFile];
        
        ComposePhotoViewController *composeVC = [[ComposePhotoViewController alloc] init];
        composeVC.photoImage = image;
        composeVC.parentVC = self;
        [self.navigationController pushViewController:composeVC animated:YES];
    }];
}

- (void)saveImage:(UIImage *)tempImage
         WithName:(NSString *)imageName {
    
    UIImage *image = [tempImage copy];
    float width = tempImage.size.width;
    float height = tempImage.size.height;
    if (width > 1000) {
        height = height/(width/1000);
        image = [tempImage scaleForSize:CGSizeMake(1000, height)];
    }
    
    NSData* imageData = UIImageJPEGRepresentation(image, 0.5);
    NSString* fullPathToFile = [AppSetting filePath:imageName];
    
    [imageData writeToFile:fullPathToFile atomically:NO];
}


@end